﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Management;

namespace ConfigAndElse
{
    /// <summary>
    /// 软件注册
    /// </summary>
    public class SoftReg
    {
        /// <summary>
        /// 软件名字代码
        /// </summary>
        public string softname="CMV1";

        /// <summary>
        /// 测试
        /// </summary>
        public void test() {             
             string machineCode = "";
             machineCode = GetMachineCode();
             string sucsess = GetRegKey(machineCode);  
             Console.WriteLine(sucsess);                      
        }

        /// <summary>
        /// 注册码检查
        /// </summary>
        /// <param name="regKey"></param>
        /// <returns></returns>
        public bool ChkRegKey(string regKey)
        {
            string machineCode = GetMachineCode();
            return ChkRegKey(machineCode, regKey);        
        }

        /// <summary>
        /// 注册码检查
        /// </summary>
        /// <param name="machineCode"></param>
        /// <param name="regKey"></param>
        /// <returns></returns>
        public bool ChkRegKey(string machineCode,string regKey)
        {
            bool result = false;
            if(GetRegKey(machineCode)==regKey){
                result = true;
            }
            return result;
        }

        /// <summary>
        /// 取得注册码
        /// </summary>
        /// <param name="machineCode">机器码</param>
        /// <returns></returns>
        public string GetRegKey(string machineCode)
        {
            string str = "";
            //软件名SHA加密生成注册码组成部分的
            SHA1 sha = new SHA1CryptoServiceProvider();
            str = BitConverter.ToString(sha.ComputeHash(Encoding.Default.GetBytes(softname))).Replace("-", "").ToUpper();

            //电脑标识SHA加密生成注册码组成部分的
            SHA1 sha2 = new SHA1CryptoServiceProvider();
            string str4 = BitConverter.ToString(sha2.ComputeHash(Encoding.Default.GetBytes(machineCode))).Replace("-", "").ToUpper();

            string str5 = "";
            for (int i = 0; i < str4.Length; i++)
            {
                if ((i % 2) == 1)
                {
                    str5 = str5 + str4[i];
                }
            }

            string str6 = "";
            for (int j = 0; j < str.Length; j++)
            {
                if ((j % 2) == 0)
                {
                    str6 = str6 + str[j];
                }
            }

            int[] numArray = new int[20];

            for (int k = 0; k < 20; k++)
            {

                numArray[k] = str6[k] + str5[k];

                numArray[k] = numArray[k] % 0x24;

            }

            string str7 = "";

            for (int m = 0; m < 20; m++)
            {

                if ((m > 0) && ((m % 4) == 0))
                {

                    str7 = str7 + "-";

                }

                str7 = str7 + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[numArray[m]];

            }

            return str7;

        }


        /// <summary>
        /// 取得系统机器码
        /// </summary>
        /// <returns></returns>
        public string GetMachineCode()
        {

            string cpuID = this.GetCpuID();
            if (cpuID != "")
            {

                MD5 md = new MD5CryptoServiceProvider();

                cpuID = BitConverter.ToString(md.ComputeHash(Encoding.Default.GetBytes(cpuID))).Replace("-", "").ToUpper().Substring(8, 0x10);

                return (softname + "C" + cpuID);

            }

            string bIOSSerialNumber = this.GetBIOSSerialNumber();
            if (bIOSSerialNumber != "")
            {

                MD5 md2 = new MD5CryptoServiceProvider();

                bIOSSerialNumber = BitConverter.ToString(md2.ComputeHash(Encoding.Default.GetBytes(bIOSSerialNumber))).Replace("-", "").ToUpper().Substring(8, 0x10);

                return (softname + "B" + bIOSSerialNumber);

            }

            string hardDiskSerialNumber = this.GetHardDiskSerialNumber();
            if (hardDiskSerialNumber != "")
            {

                MD5 md3 = new MD5CryptoServiceProvider();

                hardDiskSerialNumber = BitConverter.ToString(md3.ComputeHash(Encoding.Default.GetBytes(hardDiskSerialNumber))).Replace("-", "").ToUpper().Substring(8, 0x10);

                return (softname + "H" + hardDiskSerialNumber);

            }

            string netCardMACAddress = this.GetNetCardMACAddress();
            if (netCardMACAddress != "")
            {

                MD5 md4 = new MD5CryptoServiceProvider();

                netCardMACAddress = BitConverter.ToString(md4.ComputeHash(Encoding.Default.GetBytes(netCardMACAddress))).Replace("-", "").ToUpper().Substring(8, 0x10);

                return (softname + "N" + netCardMACAddress);

            }

            return (softname + "WF53A419DB238BBAD");

        }


        /// <summary>
        /// 取得系统BIOSS号码
        /// </summary>
        /// <returns></returns>
        public string GetBIOSSerialNumber()
        {

            try
            {

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");

                string str = "";

                foreach (ManagementObject obj2 in searcher.Get())
                {

                    str = obj2["SerialNumber"].ToString().Trim();

                }

                return str;

            }

            catch
            {

                return "";

            }

        }      


        /// <summary>
        /// 取得CPU　ID
        /// </summary>
        /// <returns></returns>
        public string GetCpuID()
        {

            try
            {

                ManagementObjectCollection instances = new ManagementClass("Win32_Processor").GetInstances();

                string str = null;

                foreach (ManagementObject obj2 in instances)
                {

                    str = obj2.Properties["ProcessorId"].Value.ToString();

                    break;

                }

                return str;

            }

            catch
            {

                return "";

            }

        }


        /// <summary>
        /// 取得硬盘序列号
        /// </summary>
        /// <returns></returns>
        public string GetHardDiskSerialNumber()
        {

            try
            {

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

                string str = "";

                foreach (ManagementObject obj2 in searcher.Get())
                {

                    str = obj2["SerialNumber"].ToString().Trim();

                    break;

                }

                return str;

            }

            catch
            {

                return "";

            }

        }


        /// <summary>
        /// 取得网卡地址
        /// </summary>
        /// <returns></returns>
        public string GetNetCardMACAddress()
        {

            try
            {

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");

                string str = "";

                foreach (ManagementObject obj2 in searcher.Get())
                {

                    str = obj2["MACAddress"].ToString().Trim();

                }

                return str;

            }

            catch
            {

                return "";

            }

        }

        /// <summary>
        /// 系统信息
        /// </summary>
        private void GetInfo()
        {
            string cpuInfo = "";//cpu序列号 

            ManagementClass cimobject = new ManagementClass("Win32_Processor");
            ManagementObjectCollection moc = cimobject.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                //Response.Write("cpu序列号：" + cpuInfo.ToString());
            }

            //获取硬盘ID 
            String HDid;
            ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
            ManagementObjectCollection moc1 = cimobject1.GetInstances();
            foreach (ManagementObject mo in moc1)
            {
                HDid = (string)mo.Properties["Model"].Value;
              //  Response.Write("硬盘序列号：" + HDid.ToString());
            }
            //获取网卡硬件地址 
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if ((bool)mo["IPEnabled"] == true)
               //     Response.Write("MAC address\t{0}" + mo["MacAddress"].ToString());
                mo.Dispose();
            }
        }
      

    } 
}
